package com.ll.wechat.friends.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jivesoftware.smackx.search.ReportedData.Row;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ll.wechat.R;
import com.ll.wechat.base.activity.BaseActivity;
import com.ll.wechat.base.app.App;
import com.ll.wechat.base.app.Constant;
import com.ll.wechat.base.net.NetCallBackListener;
import com.ll.wechat.base.net.NetUtil;
import com.ll.wechat.base.utils.widget.LoadingDialog;
import com.ll.wechat.base.utils.widget.MyToast;
import com.ll.wechat.friends.adapter.AddFriendsAdapter;
import com.ll.wechat.friends.utils.SearchEditext;
import com.ll.wechat.friends.utils.SearchEditext.Listener;

/**
 * 添加好友页面
 * 
 * @ClassName: AddFriendsActivity
 * @author yeliangliang
 * @date 2015-8-24 下午8:44:30
 */
public class AddFriendsActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {

	private TextView tv_add;// 添加好友按钮
	private ListView listView;// 搜索结果列表
	private AddFriendsAdapter mAdapter;
	private List<Row> mList;
	private String keyStr = "";
	private ActionBar actionBar;
	private RelativeLayout img_back;
	private SearchEditext edt_search;
	private LoadingDialog dialog;
	private String name;

	@Override
	public void initView() {
		initActionBar();
		setContentView(R.layout.activty_add_friends);
		listView = (ListView) findViewById(R.id.list_add_friends);
		listView.setOnItemClickListener(this);
		tv_add = (TextView) findViewById(R.id.tv_add);
		tv_add.setOnClickListener(this);
		tv_add.setVisibility(View.GONE);
	}

	/**
	 * 初始化ActionBar
	 * 
	 * @author yeliangliang
	 * @date 2015-8-26 下午10:45:18
	 * @version V1.0
	 * @return void
	 */
	private void initActionBar() {
		actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setCustomView(R.layout.action_add_friends);// 自定义ActionBar布局
		edt_search = (SearchEditext) actionBar.getCustomView().findViewById(R.id.edt_search);
		img_back = (RelativeLayout) actionBar.getCustomView().findViewById(R.id.rl_img_action_back);
		img_back.setOnClickListener(this);
		edt_search.setListener(new Listener() {

			@Override
			public void didClearText() {
				// 查询
				if (!NetUtil.isNetConnection()) {
					MyToast.showToast(AddFriendsActivity.this, "请检查网络连接");
					return;
				}
				if (dialog == null) {
					dialog = new LoadingDialog(AddFriendsActivity.this,"正在查找");
				}
				dialog.showDialog();
				searchFriend();
			}
		});
	}

	@Override
	public void initData() {
		mList = new ArrayList<Row>();
		mAdapter = new AddFriendsAdapter(this, mList);
		listView.setAdapter(mAdapter);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_img_action_back:
			finishActivity(AddFriendsActivity.this);
			break;
		case R.id.tv_add:
			addFriend();
			break;
		default:
			break;
		}
	}

	/**
	 * 搜索朋友
	 * 
	 * @author yeliangliang
	 * @date 2015-8-24 下午8:41:19
	 * @version V1.0
	 * @return void
	 */
	private void searchFriend() {
		name = "";
		keyStr = edt_search.getText().toString();
		NetUtil.searchFriends(AddFriendsActivity.this, keyStr, new NetCallBackListener() {

			@Override
			public void responseSuccess(int i, Object o) {
				handlerSearch.obtainMessage(i, o).sendToTarget();
			}

			@Override
			public void responseFailed(int i) {
				handlerSearch.obtainMessage(i, "").sendToTarget();
			}

			@Override
			public void responseError(int i) {
				handlerSearch.obtainMessage(i, "").sendToTarget();
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		name = mList.get(position).getValues("Name").get(0);
		mAdapter.selectPosition = position;
		mAdapter.notifyDataSetChanged();
		tv_add.setVisibility(View.VISIBLE);
	}

	/**
	 * 添加好友
	 * 
	 * @author yeliangliang
	 * @date 2015-8-26 下午11:01:59
	 * @version V1.0
	 * @return void
	 */
	private void addFriend() {
		if (name.equals("")) {
			return;
		}
		if (dialog == null) {
			dialog = new LoadingDialog(AddFriendsActivity.this,"正在请求");
		}
		dialog.showDialog();
		NetUtil.addFriends(AddFriendsActivity.this, name, new NetCallBackListener() {

			@Override
			public void responseSuccess(int i, Object o) {
				handlerAdd.obtainMessage(i).sendToTarget();
			}

			@Override
			public void responseFailed(int i) {
				handlerAdd.obtainMessage(i).sendToTarget();
			}

			@Override
			public void responseError(int i) {

			}
		});
	}

	// 搜索好友
	private Handler handlerSearch = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mList.clear();
			switch (msg.what) {
			case Constant.REQUEST_SUCCESS:
				// 搜索成功
				mList.addAll((Collection<? extends Row>) msg.obj);
				tv_add.setVisibility(View.GONE);
				mAdapter.selectPosition = -1;
				mAdapter.notifyDataSetChanged();
				break;
			case Constant.REQUEST_ERROR:
				// 搜索失败
				break;
			case Constant.REQUEST_FAILD:
				// 返回为空
				MyToast.showToast(AddFriendsActivity.this, "搜索结果为空");
				break;
			default:
				break;
			}
			mAdapter.notifyDataSetChanged();
			if (dialog != null) {
				dialog.cancel();
			}
		}

	};
	// 添加好友
	private Handler handlerAdd = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Constant.REQUEST_SUCCESS:
				// 添加成功
				MyToast.showToast(AddFriendsActivity.this, "添加成功");
				// 发送广播--更新好友列表
				Intent intent = new Intent(Constant.RECIVER_DELETE_FRIENDS);
				App.mContext.sendBroadcast(intent, "com.wechat.permission.RECIVER_MESSAGE");
				finish();
				break;
			case Constant.REQUEST_FAILD:
				// 添加失败
				MyToast.showToast(AddFriendsActivity.this, "好友添加失败");
				break;
			default:
				break;
			}
			if (dialog != null) {
				dialog.cancel();
			}
		}

	};
}
